﻿using System;
using Model;
using Service;
using Resources;

public partial class planningEdit : System.Web.UI.Page
{
    Utility _Utility = new Utility();
    PlanningService _PlanningService = new PlanningService();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            int planningId = _Utility.ConvertToInteger(Session["planningId"]);
            if (planningId == 0) pnlBody.Visible = false;
            else
            {
                pnlBody.Visible = true;
                if (Session["planning"] == null) DisplayDetail(planningId);
                else
                {
                    DisplayDetail((Planning) Session["planning"]);
                }
            }
            if (Session["planning"] != null)
            {
                pnlBody.Visible = true;
                DisplayDetail((Planning)Session["planning"]);
               
            }
            
            ClearSessions();
        }

    }
    private void ClearSessions()
    {
        Session["planning"] = null;
        Session["planTime"] = null;
        Session["oringalPage"] = null;
    }


    private void CallDataBack()
    {
        int planningId = _Utility.ConvertToInteger(Request.QueryString["planningId"]);
        pnlBody.Visible = true;
        DisplayDetail(planningId);
    }

    private void DisplayDetail(int planningId)
    {
        try
        {
            lblHeader.Text = Resource.lblPlanning + planningId;
            Planning planning = _PlanningService.GetPlanningByPlanningId(planningId);

            txtRemark.Text = planning.Remark;
            txtPlanTime.Text = planning.PlanningTime.Hour + ":" + planning.PlanningTime.Minute;
            txtPlanDate.Text = planning.PlanningDate.ToString(Resource.DateEnglishFormat);
            txtEstimatedTime.Text = planning.EstimateTimeInMinute != 0
                                        ? planning.EstimateTimeInMinute.ToString()
                                        : string.Empty;

            drlPriority.SelectedValue = planning.Priority.ToString();
            drlITStaff.SelectedValue = planning.EmployeeId != 0 ? planning.EmployeeId.ToString(): (-1).ToString();
            

            // hidden file
            txtHiddenPlanningId.Text =planning.PlanningId.ToString();
            txtHiddenTaskId.Text = planning.TaskId.ToString();

            WorkOrderTask woTask  = new WorkOrderService().GetTaskWorkOrder(planning.TaskId);
            txtWOID.Text = woTask.WorkOrderId.ToString();

        }catch(Exception ex)
        {
            lblInformation.Text = ex.Message;
        }

    }
    private void DisplayDetail(Planning planning)
    {
        try
        {
            lblHeader.Text = Resource.lblPlanning + planning.PlanningId;
           
            txtRemark.Text = planning.Remark;
            txtPlanTime.Text = planning.PlanningTime.Hour + ":" + planning.PlanningTime.Hour;
            txtPlanDate.Text = planning.PlanningDate.ToString(Resource.DateEnglishFormat);

            if (Session["planTime"] != null)
            {
                txtPlanTime.Text = ((DateTime)Session["planTime"]).Hour + " : " + ((DateTime)Session["planTime"]).Minute;
                txtPlanDate.Text = new Utility().ConvertToDisplayDateString((DateTime)Session["planTime"]);
            }
            txtEstimatedTime.Text = planning.EstimateTimeInMinute != 0
                                        ? planning.EstimateTimeInMinute.ToString()
                                        : string.Empty;

            drlPriority.SelectedValue = planning.Priority.ToString();
            drlITStaff.SelectedValue = planning.EmployeeId != 0 ? planning.EmployeeId.ToString() : (-1).ToString();

            // hidden file
            txtHiddenPlanningId.Text = planning.PlanningId.ToString();
            txtHiddenTaskId.Text = planning.TaskId.ToString();
            WorkOrderTask woTask = new WorkOrderService().GetTaskWorkOrder(planning.TaskId);
            txtWOID.Text = woTask.WorkOrderId.ToString();

        }
        catch (Exception ex)
        {
            lblInformation.Text = ex.Message;
        }

    }
    private Planning GetPlanning(int taskId)
    {
        Utility _Uitility = new Utility();
        Planning planning = new Planning();

        planning.EmployeeId = Int32.Parse(drlITStaff.SelectedValue);
        planning.EstimateTimeInMinute = !String.IsNullOrEmpty(txtEstimatedTime.Text) ? _Uitility.ConvertToInteger(txtEstimatedTime.Text) : 0;
        planning.PlanningDate = new Utility().ConvertToCustomizedFormat(txtPlanDate.Text);

        if (!String.IsNullOrEmpty(txtPlanTime.Text))
            planning.PlanningTime = DateTime.Parse(txtPlanTime.Text);
        else planning.PlanningTime = DateTime.Parse(DateTime.Now.Hour + ":" + DateTime.Now.Minute);

        planning.Priority = Int32.Parse(drlPriority.SelectedValue);
        planning.Remark = txtRemark.Text;
        planning.TaskId = taskId;
        planning.PlanningId = _Utility.ConvertToInteger(Session["planningId"]);
        
        return planning;

    }
    protected void btnValidateWorkOrder_Click(object sender, EventArgs e)
    {
        if (!_Utility.TestPermissions(Resource.PermissionEditPlanning))
        {
            lblMessageValidate.Text = Resource.Permission;
            CallDataBack();
            return;
        }
        try
        {
           if (!ValidateData()) return;
           Planning planning = GetPlanning(_Utility.ConvertToInteger(txtHiddenTaskId.Text));
           new PlanningService().SavePlanning(planning);
           lblInformation.Text = Resource.MessageValidate;
        }
        catch(Exception ex)
        {
            throw ex;
        }

    }
    private bool ValidateData()
    {
        if (drlITStaff.SelectedIndex <= 0)
        {
            lblRequiredEmployee.Text = Resource.MessInformSelectAValue;
            return false;
        }
        lblRequiredEmployee.Text = string.Empty;
       

        if (String.IsNullOrEmpty(txtPlanDate.Text))
        {
            lblRequiredPlanDate.Text = Resource.MsgFillInformation;
            return false;
        }
        lblRequiredPlanDate.Text = string.Empty;

        if (String.IsNullOrEmpty(txtPlanTime.Text))
        {
            lblRequiredPlanTime.Text = Resource.MsgFillInformation;
            return false;
        }
        lblRequiredPlanTime.Text = string.Empty;

        return true;

    }
    private void ClearWorkOrderFields()
    {
        txtRemark.Text = string.Empty;
        txtPlanTime.Text = string.Empty;
        txtEstimatedTime.Text = string.Empty;
        drlITStaff.SelectedIndex = -1;
        drlPriority.SelectedIndex = -1;
        txtPlanDate.Text = string.Empty;
    }
    protected void btnCancelWorkOrder_Click(object sender, EventArgs e)
    {
        ClearWorkOrderFields();
    }
    protected void drlITStaff_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    private void LoadPlanning ()
    {
        if (drlITStaff.SelectedIndex > 0)
        {
            Planning planning = GetPlanning(_Utility.ConvertToInteger(txtHiddenTaskId.Text));
            planning.PlanningId = _Utility.ConvertToInteger(Request.QueryString["planningId"]);
            Session["planning"] = planning;
            Session["oringalPage"] = "./planningEdit.aspx";
            lblInformation.Text = string.Empty;
            
            Response.Redirect("./laborResourceView.aspx?day=" + (String.IsNullOrEmpty(txtPlanDate.Text) ? txtPlanDate.Text : DateTime.Now.ToString(Resource.DateEnglishFormat)));        
        }
        else lblInformation.Text = "Selected requested date and IT Staffs to see their schedule !";
        
    }
    protected void imgButton_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
        LoadPlanning();
    }
}
